package com.mark.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;

import com.mark.entity.StuAnswer;
import com.user.entity.Student;

public class StuAnswerDTO {
	public static Specification<StuAnswer> getSpecification(Long studentId) {
		Specification<StuAnswer> spec = new Specification<StuAnswer>() {
			public Predicate toPredicate(Root<StuAnswer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> list = new ArrayList<Predicate>();
				if(studentId!=null) {
					ListJoin<StuAnswer,Student> join= root.join(root.getModel().getList("student",Student.class),JoinType.LEFT);
					Predicate predicate = cb.equal(join.get("id").as(Long.class),studentId);
					list.add(predicate);
				}else {
					
				}
				
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
}
